package org.oims.simpleserver.schedule;

import org.oims.simpleserver.services.MessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

import java.util.Calendar;

@Controller
public class SimpleScheduledTask {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    private MessageService messageService;

    @Scheduled(fixedDelay = 43200000, initialDelay = 15000)
//    @Scheduled(fixedDelay = 5000, initialDelay = 10000)
    public void clearOutDateFriendMsg(){
        try {
            logger.info("Clearing out-date friend msg");
            Calendar time = Calendar.getInstance();

            int nowDayOfMonth = time.get(Calendar.DAY_OF_MONTH);
            int dayNum = 10;
            time.set(Calendar.DAY_OF_MONTH, nowDayOfMonth - dayNum);

            time.set(Calendar.HOUR_OF_DAY, 0);
            time.set(Calendar.MINUTE, 0);
            time.set(Calendar.SECOND, 0);
            time.set(Calendar.MILLISECOND, 0);
            //删去dayNum天之前的所有好友消息
            messageService.clearOutDateFriendMsgs(time);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Autowired
    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }
}
